package com.mangowang.lib.common.net;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.mangowang.lib.common.BuildConfig;
import com.mangowang.lib.common.help.UserHelper;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.net.interfaces.OnUpLoadCallBack;
import com.mangowang.lib.common.net.request.RequestModel;
import com.mangowang.lib.common.proto.PBPublicOuterClass;
import com.mangowang.lib.common.util.MMKVUtil;
import com.mangowang.lib.common.util.ValidateUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by intexh on 2016/6/4.
 */
public enum NetworkManager {
    INSTANCE;
    public  String SERVER_HEAD = "SERVER_HEAD";
    public void init(Application application) {
        //必须调用初始化
        OkGo.getInstance().init(application);
//        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        //全局的读取超时时间
//        builder.readTimeout(15000, TimeUnit.MILLISECONDS);
//        //全局的写入超时时间
//        builder.writeTimeout(15000, TimeUnit.MILLISECONDS);
//        //全局的连接超时时间
//        builder.connectTimeout(15000, TimeUnit.MILLISECONDS);
    }

    //请求 post
    public void post(String api, Map<String, String> params, OnRequestCallBack callBack) {

        if (TextUtils.isEmpty(MMKVUtil.getString(SERVER_HEAD))) {
            RequestModel.INSTANCE.post("", BuildConfig.BASE_URL + api, params, callBack);
        } else {
            RequestModel.INSTANCE.post("", MMKVUtil.getString(SERVER_HEAD) + api, params, callBack);
            Log.e("serviceHost", MMKVUtil.getString(SERVER_HEAD));
        }
    }

    //请求 get
    public void get(String api, Map<String, String> params, OnRequestCallBack callBack) {
        if (TextUtils.isEmpty(MMKVUtil.getString(SERVER_HEAD))) {
            RequestModel.INSTANCE.get("", BuildConfig.BASE_URL + api, params, callBack);
        } else {
            RequestModel.INSTANCE.get("", MMKVUtil.getString(SERVER_HEAD) + api, params, callBack);

        }
    }

    //请求文件上传
    public void upLoadFile(String api, String key, File file, OnUpLoadCallBack callBack) {
        RequestModel.INSTANCE.upLoadFile(api, key, file, callBack);
    }

    //请求文件上传
//    public void upLoadMultiFile(List<LocalMedia> images, OnUpLoadCallBack callBack) {
//        List<String> upLoadSuccessImages = new ArrayList<>();
//        if (TextUtils.isEmpty(MMKVUtil.getString(SERVER_HEAD))) {
//            RequestModel.INSTANCE.upLoadMultiFile(BuildConfig.BASE_URL + Apis.uploadImage, "file",upLoadSuccessImages,
//                    images,new File(images.get(0).getCompressPath()),0,callBack);
//        } else {
//            RequestModel.INSTANCE.upLoadMultiFile(MMKVUtil.getString(SERVER_HEAD) + Apis.uploadImage, "file",upLoadSuccessImages,
//                    images,new File(images.get(0).getCompressPath()),0,callBack);
//        }
//    }




    //请求 post
    public void post(String api, byte[] data, int page, OnRequestCallBack callBack) {
        Map<String, String> params = new HashMap<>();
        if (ValidateUtils.isValidate(data)) {
            params.put("data", byteArrayToString(data));
        }
        // 公共方法
        PBPublicOuterClass.PBPublic.Builder builder = PBPublicOuterClass.PBPublic.newBuilder();
        if (UserHelper.isLogin()) builder.setKey(UserHelper.getCurrentToken());
        builder.setClient("android");
        if (page != 0) {
            builder.setPage(page);
            builder.setPagesize(10);
        }
        PBPublicOuterClass.PBPublic public_data = builder.build();
        params.put("dtp", byteArrayToString(public_data.toByteArray()));
        if (TextUtils.isEmpty(MMKVUtil.getString(SERVER_HEAD))) {
            RequestModel.INSTANCE.post("", BuildConfig.BASE_URL + api, params, callBack);
        } else {
            RequestModel.INSTANCE.post("", MMKVUtil.getString(SERVER_HEAD) + api, params, callBack);
            Log.e("serviceHost", MMKVUtil.getString(SERVER_HEAD));
        }
    }

    //请求 post
    public void post(String api, byte[] data, OnRequestCallBack callBack) {
        post(api, data, 0, callBack);  // 默认为 0 的时候不给 Page pagesize等公共参数
    }

    //请求 post
    public void post(String api, OnRequestCallBack callBack) {
        post(api, null, 0, callBack);  // 默认为 0 的时候不给 Page pagesize等公共参数
    }

    public void post(String api, int page, OnRequestCallBack callBack) {
        post(api, null, page, callBack);  // 默认为 0 的时候不给 Page pagesize等公共参数
    }

    private String byteArrayToString(byte[] bytes) {
        StringBuilder stringBuffer = new StringBuilder();
        for (byte aByte : bytes) {
            stringBuffer.append((int) aByte).append(",");
        }
        stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        return stringBuffer.toString();
    }

}
